const util = require('util');
const debug = util.debuglog('oil-game:exchange:api');

// 获取石油和金钱
exports.userMoneyAndOil = function (session) {
  let {uid} = session;
  debug("uid = %s 获取石油和金钱", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.userMoneyAndOil(session, uid, function (err, data) {
      if (err) {
        debug("uid = %s  获取石油和金钱异常  %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s 获取石油和金钱成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

// 石油增加
exports.addOil = function (session, addOilNum) {
  let {uid, frontendId} = session;
  debug("uid = %s 石油增加", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.addOil(session, uid, frontendId, addOilNum, "exchange", '',function (err, data) {
      if (err) {
        debug("uid = %s  石油增加异常  %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s 石油增加成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

// 石油减少
exports.reduceOil = function (session, reduceOilNum) {
  let {uid, frontendId} = session;
  debug("uid = %s 石油减少", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.reduceOil(session, uid, frontendId, reduceOilNum, "exchange", function (err, data) {
      if (err) {
        debug("uid = %s  石油减少异常  %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s 石油减少成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

// 金钱增加
exports.addMoney = function (session, money) {
  let {uid, frontendId} = session;
  debug("uid = %s 金钱增加", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.addMoney(session, uid, frontendId, money, "exchange", function (err, data) {
      if (err) {
        debug("uid = %s 金钱增加异常 %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s 金钱增加成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

// 获取用户等级
exports.getUserGrade = function (session) {
  let {uid} = session;
  debug("uid = %s 获取用户等级", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.getUserGrade(session, uid, function (err, data) {
      if (err) {
        debug("uid = %s 获取用户等级异常 %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s 获取用户等级成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

// 获取用户等级限制
exports.getUserGradeLimit = function (session) {
  let {uid} = session;
  debug("uid = %s 获取用户等级限制信息", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.getUserGradeLimit(session, uid, function (err, data) {
      if (err) {
        debug("uid = %s 获取用户等级异常 %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s 获取用户等级成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}
